﻿using AppWebApi.Models;
using Microsoft.AspNetCore.Mvc;
using Modules.App;
using PmSoft.ApplicationContext;
using PmSoft.Utilities;

namespace AppWebApi.Controllers
{
    /// <summary>
    /// App工具相关接口
    /// </summary>
    [ApiController]
    [Route("/api/v1/app")]
    public class CommonController : ControllerBase
    {
        private readonly IPSearcher iPSearcher;
        private readonly IApplicationContext applicationContext;
        private readonly IConfiguration configuration;

        private readonly AppVersionService appVersionService;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="iPSearcher"></param>
        /// <param name="applicationContext"></param>
        public CommonController(IPSearcher iPSearcher, IApplicationContext applicationContext, IConfiguration configuration, AppVersionService appVersionService)
        {
            this.applicationContext = applicationContext;
            this.iPSearcher = iPSearcher;
            this.configuration = configuration;
            this.appVersionService = appVersionService;
        }

        /// <summary>
        /// 获取当前登录人ip地址
        /// </summary>
        /// <returns></returns>
        [HttpGet("getLocationInfo")]
        public object GetLocationInfo()
        {
            var cloentIp = iPSearcher.GetLocationInfo(applicationContext.ClientIp ?? string.Empty);
            return ApiResult.Success("成功", cloentIp).Encrypt();
        }

        /// <summary>
        /// 获取app版本信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("getVersion")]
        public ApiResult GetVersion()
        {
            var appVersion = appVersionService.GetAll();
            if (appVersion == null||!appVersion.Any()||appVersion.Count()==0) return ApiResult.Failure("没有查询到任何数据").Encrypt();
            return ApiResult.Success("成功", appVersion.OrderBy(a => a.VersionCode).FirstOrDefault());
        }
    }
}
